﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ManageProduct : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMainDataList();
            }
        }

        private void BindMainDataList()
        {
            ProductService service = new ProductService();
            List<Product> datas = service.GetAllProducts(); ;
            mainGridView.DataSource = datas;
            mainGridView.DataBind();

            RecordsNumber = null != datas ? datas.Count() : 0;
        }

        protected void mainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= mainGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (mainGridView.PageIndex + 1).ToString();
            }
        }

        protected void mainGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            hfProductID.Value = e.CommandArgument.ToString();
            if (e.CommandName == "LookProduct")
            {
                modifyPopupExtender.Show();
                BindProvider();
                BindProductType();
                ProductService service = new ProductService();
                Product product = service.GetProductByProductID(long.Parse(e.CommandArgument.ToString()));
                MaterialNOTxtBox.Text = product.MaterialID;
                ModelNOTxtBox.Text = product.ModelNumber;
                VersionNOTxtBox.Text = product.VersionNumber;
                if (!product.ProductClassReference.IsLoaded)
                {
                    product.ProductClassReference.Load();
                }
                ProductTypeDropdownList.SelectedValue = product.ProductClass.ID.ToString();
                PropertyDropdownList.SelectedValue = product.Property;
                if (!product.ProviderReference.IsLoaded)
                {
                    product.ProviderReference.Load();
                }
                ProviderDropdownList.SelectedValue = product.Provider.ProviderID.ToString();
                RepairPeriodTxtBox.Text = product.RepairPeriod.ToString();
                DescriptionTxtBox.Text = product.Description;
            }
            else if (e.CommandName == "DeleteProduct")
            {
                ProductService service = new ProductService();
                service.Delete(Convert.ToInt32(e.CommandArgument));
                BindMainDataList();
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            ProductService service = new ProductService();
            ProviderService providerService = new ProviderService();
            ProductClassService classService = new ProductClassService();
            Product editProduct = service.GetProductByProductID(long.Parse(hfProductID.Value));
            editProduct.Description = DescriptionTxtBox.Text;
            editProduct.RepairPeriod = Convert.ToInt32(RepairPeriodTxtBox.Text);
            editProduct.Provider = providerService.GetProviderByID(Convert.ToInt32(ProviderDropdownList.SelectedValue));
            editProduct.Property = PropertyDropdownList.SelectedValue;
            editProduct.ProductClass = classService.GetProductClass(Convert.ToInt32(ProductTypeDropdownList.SelectedValue));
            editProduct.ModelNumber = ModelNOTxtBox.Text;
            editProduct.MaterialID = MaterialNOTxtBox.Text;

            service.Update(editProduct);

            BindMainDataList();
        }

        protected void mainGridView_PageIndexChanged(object sender, EventArgs e)
        {
        }

        protected void mainGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < mainGridView.PageCount)
            {
                mainGridView.PageIndex = e.NewPageIndex;
            }
            
            BindMainDataList();
        }

        private void BindProvider()
        {
            ProviderService providerService = new ProviderService();
            ProviderDropdownList.DataSource = providerService.GetAllSimpleProvider();
            ProviderDropdownList.DataTextField = "ProviderName";
            ProviderDropdownList.DataValueField = "ProviderID";
            ProviderDropdownList.DataBind();
        }

        private void BindProductType()
        {
            //ApplicationProductService service = new ApplicationProductService();
            ProductClassService classService = new ProductClassService();
            ProductTypeDropdownList.DataSource = classService.GetAllClass();
            ProductTypeDropdownList.DataTextField = "Name";
            ProductTypeDropdownList.DataValueField = "ID";
            ProductTypeDropdownList.DataBind();
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            mainGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindMainDataList();
        }
    }
}
